import {
  list as signInList,signIn
} from '../../../api/signIn'
import {
  list as signInRecordList
} from '../../../api/signInRecord'

const util = require('../../../utils/util.js')
const app = getApp();
Page({
  data: {
     signInGiftList:[],
     isSignIn:false,
     banner_image:'https://rzico-weex.oss-cn-hangzhou.aliyuncs.com/img/signIn1.png',
     checkRecord:[],
     signInRecord:[],
     cycle:'',
     signInId:"1",
     promotionForm1:[],
      newPromotion1: false,
  },
      //    {
    //   amount: 30,a
    //   endDate: "2020-07-30 0:0:0",
    //   id: "28",
    //   name: "满100减30",
    //   scope: 0,
    //   tag: "全场通用",
    //   targetType: 0,
    //   type: 0,
    // },{
    //   amount: 30,
    //   endDate: "2020-07-30 0:0:0",
    //   id: "28",
    //   name: "满100减30",
    //   scope: 0,
    //   tag: "全场通用",
    //   targetType: 0,
    //   type: 0,
    // } 
  onLoad: function (options) {
      // this.getList()
      this.getdata()
      // this.popScreenClick1()
  },

  signInClick(){
    var _this = this
    signIn({signInId:this.data.signInId}).then(res=>{
        if(res.data.signInRecord.signInGift.type == 1){
          _this.data.isSignIn = false
          _this.setData({
            isSignIn:_this.data.isSignIn
          })
          _this.getdata()
          this.data.newPromotion1 = true
          this.setData({
            newPromotion1:this.data.newPromotion1
          })
          if (res.data.couponCodeVoList.length > 0) {
            res.data.couponCodeVoList.forEach(function (item) {
              item.endDate = _this.formatDateAndTime(item.endDate)
            })
            _this.data.promotionForm1 = res.data.couponCodeVoList
            this.setData({
              promotionForm1:this.data.promotionForm1
            })
            console.log(this.data.promotionForm1,"123321")
          }

          console.log(this.data.promotionForm1,"promotionForm1")
        }else{
          _this.data.isSignIn = false
          _this.setData({
            isSignIn:_this.data.isSignIn
          })
          _this.getdata()
          this.data.newPromotion1 = true
          this.setData({
            newPromotion1:this.data.newPromotion1
          })

          console.log("0000000")
          
        }


        }).catch(res=>{ 
          if(res.content == "今日已签到"){
            _this.data.isSignIn = true
            _this.setData({
              isSignIn:_this.data.isSignIn
            })

          }
        })
   },
    getList() {

      var _this = this
      signInList().then(res=>{
        console.log(res,"123")
        if(res.data.data.length>0){
          _this.data.cycle = res.data.data[0].cycle
          _this.data.signInId = res.data.data[0].id
          _this.data.signInGiftList = res.data.data[0].signInGiftList
          _this.setData({
            cycle: _this.data.cycle,
            signInId:_this.data.signInId,
            signInGiftList:_this.data.signInGiftList,
          })
        }
      })

      signInRecordList().then(res=>{
        _this.data.isSignIn = res.data.isSignIn
        _this.data.signInRecord = res.data.signInRecord      
        // _this.data.signInId = res.data.signInRecord[0].signInId   
        console.log(res,"res")       
        _this.setData({
          isSignIn: _this.data.isSignIn,
          signInRecord:_this.data.signInRecord,
          // signInId:_this.data.signInId,
        })
      })
  },

  
  getdata(){

    new Promise((resolve, reject) => {
      // setTimeout(function () {
      // }, 1000)
      resolve() ;
      this.getList()
    }).then((data) => { // 因为new Promise返回的就是一个promise对象实例，所以这里可以链式操作

      var _this = this
      // console.log("123")
      setTimeout(()=>{
        // let i =0
        // let signQuantity = _this.data.cycle - _this.data.signInRecord.length
        // for( i=0; i<signQuantity;i++){
        //    _this.data.signInRecord.push({})
        // }
        // _this.setData({
        //   signInRecord:_this.data.signInRecord
        // })


         console.log( _this.data.signInRecord,"4123")
          let i = 0
          for(i=0; i<_this.data.signInRecord.length;i++){
            _this.data.signInGiftList[i] =  _this.data.signInRecord[i].signInGift;
            _this.data.signInGiftList[i].isSignIn = true;
          }

          _this.setData({
            signInGiftList: _this.data.signInGiftList,
          })



      },1000)
    })

  },
  //时间格式化器
  formatDateAndTime: function (value) {
    let date = new Date(value);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    return y + '-' + MM + '-' + d + " " + h + ":" + m + ":" + s;
  },

  // 获取签到活动
  popScreenClick1() {
    var _this = this
    signInList().then(res=>{
      console.log(res,"32131")
      if(res.data.data[0])
      this.data.newPromotion1 = true
      _this.data.promotionForm1 = res.data.data[0].signInGiftList
      this.setData({
        newPromotion1: this.data.newPromotion1,
        promotionForm1: this.data.promotionForm1
      })
      console.log(this.data.promotionForm1,"123")
    })
  },
  canclePromotion1() {
    var _this = this
    _this.data.newPromotion1 = false
    _this.setData({
      newPromotion1: _this.data.newPromotion1
    })
  },
  clickAndGetClick1() {
    this.data.newPromotion1 = false
    this.setData({
      newPromotion1: this.data.newPromotion1
    })
    // wx.switchTab({
    //   url: '/pages/index/index',
    // })
    // wx.navigateTo({
    //   url: '/pages/member/function/signIn',
    // })
  },

});